package com.haier.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.haier.constant.HpConstant;
import com.haier.entity.HpConstantEntity;
import com.haier.mapper.HpAuthorizerConfigMapper;
import com.haier.mapper.HpConstantMapper;
import com.haier.utils.EmptyUtils;
import com.haier.vo.HpAreaAuditorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.haier.constant.Constant;
import com.haier.constant.URLConstant;
import com.haier.service.IhaierService;
import com.haier.utils.GetUtils;
import com.haier.utils.HttpUtils;
import com.haier.utils.Md5Util;
import com.haier.vo.ResponseVo;

@Service
public class IhaierServiceImpl implements IhaierService {

	@Autowired
	private HpAuthorizerConfigMapper hpAuthorizerConfigMapper;
	@Autowired
	private HpConstantMapper hpConstantMapper;

	@Override
	public String getQr(String jobNumber, Integer validTime, Integer entryTimes) {
		try {
			JSONObject params = new JSONObject();
			params.put("userId", jobNumber);
			params.put("validTime", validTime);
			params.put("entryTimes", entryTimes);
			Map<String, String> header = new HashMap<String, String>();
			header.put("Authorization", getAuthorization());
			header.put("appKey", Constant.IHAIER_APP_KEY);
			String result = HttpUtils.post(URLConstant.IHAIER_QR, params.toJSONString(), header);
			JSONObject resultJson = JSON.parseObject(result);
			String code = resultJson.getString("code");
			if (!"200".equals(code)) {
				return null;
			}
			return resultJson.getString("object");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public JSONObject getUserInfo(String jobNumber) {
		try {
			JSONObject params = new JSONObject();
			params.put("UserId", jobNumber);
			Map<String, String> header = new HashMap<String, String>();
			header.put("Authorization", getAuthorization());
			header.put("appKey", Constant.IHAIER_APP_KEY);
			String result = HttpUtils.post(URLConstant.IHAIER_USERINFO, params.toJSONString(), header);
			JSONObject resultJson = JSON.parseObject(result);
			HpAreaAuditorVo effectiveAuthorizedPerson = hpAuthorizerConfigMapper.findEffectiveAuthorizedPerson(jobNumber);
			String isAuthorize = HpConstant.ISENABLE_NO;
			if(!EmptyUtils.isEmpty(effectiveAuthorizedPerson)){
				isAuthorize = HpConstant.IS_AUTHORIZE_YES;
				resultJson.getJSONObject("object").put("passivityaAuthorizerNum",effectiveAuthorizedPerson.getJobNumber());
				resultJson.getJSONObject("object").put("passivityAuthorizerName",effectiveAuthorizedPerson.getName());
			}
			HpConstantEntity hpConstantEntity = hpConstantMapper.selectById(HpConstant.CONSTANT_KEY_AUTHORIZER_LEVEL);
			resultJson.getJSONObject("object").put("authorizerLevel",hpConstantEntity.getHpValue());
			resultJson.getJSONObject("object").put("isAuthorize",isAuthorize);
			return resultJson.getJSONObject("object");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public String getFace(String jobNumber) {
		try {
			JSONObject params = new JSONObject();
			params.put("UserId", jobNumber);
			params.put("UserType", "employee");
			Map<String, String> header = new HashMap<String, String>();
			header.put("Authorization", getAuthorization());
			header.put("appKey", Constant.IHAIER_APP_KEY);
			String result = HttpUtils.post(URLConstant.IHAIER_FACE, params.toJSONString(), header);
			JSONObject resultJson = JSON.parseObject(result);
			return resultJson.getJSONObject("object").getString("base64Str");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	@Override
	public ResponseVo getApproverId(String jobNumber, Integer type) throws Exception {
		JSONObject params = new JSONObject();
		HpAreaAuditorVo vo = new HpAreaAuditorVo();
		params.put("userId", jobNumber);
		params.put("approverType", type);
		Map<String, String> header = new HashMap<String, String>();
		header.put("Authorization", getAuthorization());
		header.put("appKey", Constant.IHAIER_APP_KEY);
		String result = HttpUtils.post(URLConstant.IHAIER_APPROVER, params.toJSONString(), header);
		JSONObject resultJson = JSON.parseObject(result);
		String code = resultJson.getString("code");
		if (!"200".equals(code)) {
			return ResponseVo.checkError(resultJson.getString("msg"));
		}
		String object = resultJson.getString("object");
		if (EmptyUtils.isEmpty(object)){
			return ResponseVo.success(vo);
		}
		HpAreaAuditorVo effectiveAuthorizedPerson = hpAuthorizerConfigMapper.findEffectiveAuthorizedPerson(object);
		if(!EmptyUtils.isEmpty(effectiveAuthorizedPerson)){
			effectiveAuthorizedPerson.setIsAuthorize(HpConstant.IS_AUTHORIZE_YES);
			return ResponseVo.success(effectiveAuthorizedPerson);
		}
		JSONObject json = getUserInfo(object);
		vo.setIsAuthorize(HpConstant.IS_AUTHORIZE_NO);
		vo.setJobNumber(json.get("userId").toString());
		vo.setName(json.get("userName").toString());
		return ResponseVo.success(vo);
	}

	private String getAuthorization() throws Exception {
		Date date = new Date();
		String dateStr = GetUtils.getDateString(date, "yyyy-MM-dd");
		String mdfive = Constant.IHAIER_APP_SECRET + dateStr;
		return Md5Util.md5(mdfive);
	}

}
